/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.service.AdminService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 登录
 *
 * @author 挺好的 2023年06月05日 15:21
 */
@Controller ("adminLoginController")
@RequestMapping ("/login")
@Slf4j
public class LoginController extends BaseController {

    /**
     * 管理员
     */
    @Resource (name = "adminServiceImpl")
    private AdminService adminService;

    /**
     * 是否是调试模式
     */
    @Value ("${is-debug}")
    private Boolean isDebug;

    /**
     * 进入登录页面
     *
     * @param model
     *         模型
     * @param request
     *         {@link HttpServletRequest}
     * @param response
     *         {@link HttpServletResponse}
     *
     * @return 登录页面
     */
    @GetMapping
    public String toLogin (Boolean isExpired, ModelMap model, HttpServletRequest request,
            HttpServletResponse response) {

        model.addAttribute("isDebug", this.isDebug != null && this.isDebug);

        if (this.adminService.isAuthenticated()) {
            return "redirect:/admin/admin/index";
        }

        model.addAttribute("isExpired", isExpired);
        return "/admin/login/login";
    }
}
